package com.ApachenData;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

public class LoginDataDriven {
    private ChromeDriver driver;
    private FirefoxDriver firefoxDriver;

    //用谷歌打开
    @BeforeClass
    public void SetUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
        //打开浏览器
        driver = new ChromeDriver();
        driver.manage().window().maximize();
//        driver.get("http://smp-test.yingedu.com/");
        driver.get("https://eteams.cn/");
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.findElement(By.id("top-btn-login")).click();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    }

    @DataProvider(name = "provideUser")
//    public Object[][] provideData() {
//        return new Object[][]{
//                {"User001", "123456"},
//                {"User002", "123451"}
//        };
//    }
    public Object[][] getExcelData() throws Exception {
        //创建io对象从磁盘读取文件
        FileInputStream fs = new FileInputStream("G:\\idea_java_project\\ExcelData.xlsx");

        //使用POI接收java IO从磁盘读取的整体excel文件
        XSSFWorkbook workbook = new XSSFWorkbook(fs);

        //获取Sheet页
        XSSFSheet sheet1 = workbook.getSheet("Sheet1");

        //获取到总列数
        short lastCellNum = sheet1.getRow(0).getLastCellNum();
        System.out.print("总列数：" + lastCellNum + "\t");

        //获取总行数
        int lastRowNum = sheet1.getLastRowNum();
        System.out.println("总行数：" + lastRowNum);

        //存入二维数组  行 列  x y
        Object[][] excelData = new Object[lastRowNum][lastCellNum];

        //遍历行数 i<总行数
        for (int i = 1; i < lastRowNum; i++) {
            //获取到每行的数据
            XSSFRow row = sheet1.getRow(i);

            //两列 i<总列数
            for (int j = 0; j < lastCellNum; j++) {
                //获取每个单元格的数据
                XSSFCell cell = row.getCell(j);
//                System.out.println(cell.getStringCellValue());
                //存入二维数组
                excelData[i - 1][j] = cell.getStringCellValue();

//                System.out.println(excelData[i - 1][j]);
            }
        }

        return excelData;

    }


    //dataProvider绑定上面的方法provideUser
    @Test(dataProvider = "provideUser")
    public void Login(String userName,String PassWord) throws Exception {


        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div[3]/div[1]/input")).sendKeys(userName);
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div[3]/div[2]/input")).sendKeys(PassWord);



//        JavascriptExecutor j = (JavascriptExecutor) driver;
//        j.executeAsyncScript("document.querySelector(\"#root>div>div.weapp-passport-layout-content>div > div.weapp-passport-common-login-panel-form>div.weapp-passport-ui-form-item.is-opend.weapp-passport-ui-form-item-animate.weapp-passport-ui-form-item-md.tNf9vHcuzs>input\").value=' ';");
//
////        document.querySelector("#app > div > div > div > div > form > div:nth-child(2) > div > div > input").value="";
//        Thread.sleep(5000);
////        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div[3]/div[2]/input")).clear();



    }
}
